//  action types
const INIT_COMMENTS = 'INIT_COMMENTS'
const ADD_COMMENT = 'ADD_COMMENT'
const DEL_COMMENT = 'DEL_COMMENT'

export function commentReducer(state, action) {
	if (!state) {
		state = {
			comments: [],
		}
	}
	switch (action.type) {
		case INIT_COMMENTS:
			return {
				comments: action.comment,
			}
		case ADD_COMMENT:
			return {
				comments: [action.comment, ...state.comment],
			}
		case DEL_COMMENT:
			return {
				comments: [
					...state.comments.slice(0, action.delIndex),
					...state.comment.slice(action.delIndex),
				],
			}
		default:
			return state
	}
}
export const initComments = (comments) => {
	return { type: 'INIT_COMMENTS', comments }
}
export const addComment = (comment) => {
	return { type: 'ADD_COMMENT', comment }
}
export const deleteComment = (delIndex) => {
	return { type: 'DEL_COMMENT', delIndex }
}
